import Mock from 'mockjs'
import {param2Obj} from '@/utils'

const List = []
const count = 100

const departmentList = [
  {title: '客服部', value: 1},
  {title: '产品部', value: 2},
  {title: '超级组', value: 3},
];

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    ip: Mock.Random.ip(),
    opTime: +Mock.Random.date('T'),
    username: '@first',
    'department|1': _.map(departmentList, 'title'),
    'status|1': ['published', 'draft', 'deleted'],
    log: Mock.Random.ctitle(20),
  }))
}

const InterfaceList = []

for (let i = 0; i < 6; i++) {
  InterfaceList.push(Mock.mock({
    id: '@increment',
    url: Mock.Random.url(),
    template: Mock.Random.cparagraph(),
    "open|1-2": true
  }))
}

export default {
  [api.SYS_DEPARTMENT_LIST]: () => ({
    code: 1,
    data: {
      items: departmentList
    }
  }),
  [api.SYS_LOG_LIST]: config => {
    const {username, department, startTime, endTime, ip, page = 1, limit = 20} = param2Obj(config.body);

    let mockList = List.filter(item => {
      // if (importance && item.importance !== +importance) return false;
      if (ip && item.ip !== ip) return false;
      if (username && item.username !== username) return false;
      if (department && item.department !== _.find(departmentList, {value: Number(department)}).title) return false;
      if (startTime && !moment(startTime).isBefore(item.opTime)) return false;
      if (endTime && !moment(endTime).isAfter(item.opTime)) return false;

      // if (title && item.title.indexOf(title) < 0) return false;
      return true;
    })

    // if (sort === '-id') {
    //   mockList = mockList.reverse();
    // }

    const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1));

    return {
      data: {
        total: mockList.length,
        items: pageList
      },
      code: 1,
    };
  },
  [api.SYS_CONFIGURE_GET]: () => ({
    code: 1,
    data: {
      sameIpCount: 12,
      timeout: 40,
      registerCount: 10
    }
  }),
  [api.SYS_INTERFACE_GET]: () => ({
    code: 1,
    data: InterfaceList
  })
}
